/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package montecarlo;

import view.Window;
import static java.lang.Math.max;
import java.util.*;
import model.Loader;
import model.Options;

/**
 *
 * @author Gautier & Sacha
 */
public class MonteCarlo {
    
    private static Options opt[];
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        /*double start, end;
        double result;*/
        opt = new Options[5];
        Loader.load(opt);
        new Window(opt);
    }

    public static double MonteCarloSimulation(String flag, Options opt, int nbSteps, int nbSimulations) {
        double T = opt.getT();
        double v = opt.getV();
        double b = opt.getB();
        double X = opt.getX();
        double r = opt.getR();
        double S = opt.getS();
        double dt;
        double St;
        double sum = 0;
        double Drift;
        double vSqrdt;
        int i, j, z;
        z = 0;

        dt = T / nbSteps;
        Drift = (b - v * v / 2) * dt;
        vSqrdt = v * java.lang.Math.sqrt(dt);

        if (flag.equals("Call")) {
            z = 1;
        } else if (flag.equals("Put")) {
            z = -1;
        } else {
            System.exit(-1);
        }

        for (i = 1; i < nbSimulations; i++) {
            St=MonteCarlo.OneSimulation(S, Drift, vSqrdt, nbSteps);
            sum = sum + max(z * (St - X), 0);
        }

        return java.lang.Math.exp(-r * T) * (sum / nbSimulations);
    }

    public static double OneSimulation(double S, double Drift, double vSqrdt, int nbSteps) {
        double St = S;
        int j=0;
        for (j = 1; j < nbSteps; j++) {
            St = St * java.lang.Math.exp(Drift + vSqrdt * new Random().nextGaussian());
        }
        return St;
    }

}
